<template>
  <el-dialog
    title="修改手机号"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
    width="460px"
    top="25vh"
  >
    <el-form
      ref="infoForm"
      :model="infoForm"
      :rules="rulesInfo"
      label-width="80px"
      size="mini"
      class="form-wrap"
    >
      <el-form-item label="手机号" prop="phone">
        <el-row type="flex">
          <el-col :span="15">
            <el-input
              v-model="infoForm.phone"
              placeholder="请输入要绑定的新手机号"
              v-if="isPhone"
            ></el-input>
            <template v-else>
              <div class="phone">{{ accountInfo.phone | formatMask }}</div>
              <div class="info">在下方输入验证码进行验证</div>
            </template>
          </el-col>
          <el-col :span="7" :offset="2">
            <EduVerificationCode @start="getVerifyCode('infoForm')"></EduVerificationCode>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="验证码" prop="verify">
        <el-col :span="15">
          <el-input v-model="infoForm.verify" placeholder="验证码"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('infoForm')" size="mini">下一步</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import EduVerificationCode from '@/components/EduVerificationCode'
export default {
  components: { EduVerificationCode },
  props: {
    visible: {
      type: Boolean
    }
  },
  data() {
    // 手机号码的校验
    var testPhone = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('请输入手机号'))
      } else {
        if (!this.$util.mobile(value)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }
    }
    return {
      infoForm: {
        phone: this.$store.getters['user/accountInfo'].phone,
        verify: ''
      },
      rulesInfo: {
        phone: [{ validator: testPhone, trigger: 'blur' }],
        verify: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      isPhone: false
    }
  },
  computed: {
    // 用户信息
    accountInfo() {
      return this.$store.getters['user/accountInfo']
    }
  },
  methods: {
    onSubmit(formName) {
      let _this = this
      _this.$refs[formName].validate(valid => {
        if (valid) {
          this.$api.createAccountPhone(
            {
              ...this.infoForm,
              original: this.isPhone ? 0 : 1
            },
            res => {
              _this.$message.success(res.msg)
              if (this.isPhone) {
                this.$emit('phone', res.data)
                this.$emit('update:visible', false)
                this.infoForm.phone = this.$store.getters['user/accountInfo'].phone
              } else {
                this.isPhone = true
                this.infoForm = {
                  phone: '',
                  verify: ''
                }
                this.tempLogin.canGet = true
              }
            },
            error => {
              _this.$message.error(error.msg)
            }
          )
        } else {
          return false
        }
      })
    },
    // 获取验证码
    getVerifyCode(formName) {
      var that = this
      that.$refs[formName].validateField('phone', valid => {
        if (!valid) {
          this.$api.createSmsVerify(
            {
              phone: this.isPhone
                ? this.infoForm.phone
                : that.$store.getters['user/accountInfo'].phone,
              type: 'phone'
            },
            res => {
              that.$message.success(res.msg)
            },
            error => {
              that.$message.error(error.msg)
            }
          )
        } else {
          return false
        }
      })
    },
    // 关闭
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.from-wrap {
  .phone {
    line-height: 28px;
    font-size: 12px;
    color: $text-color-secondary;
  }
  .info {
    font-size: 12px;
    color: $text-color-auxiliary;
  }
  .mag {
    font-size: 12px;
    color: $text-color-auxiliary;
  }
}
</style>
